package com.adnaan.api.controller.auth;

import cn.dev33.satoken.stp.StpUtil;
import com.adnaan.api.service.AppUserService;
import com.adnaan.common.model.Result;
import com.adnaan.common.model.dto.auth.LoginRequest;
import com.adnaan.common.model.dto.auth.LoginResponse;
import com.adnaan.common.model.dto.auth.RegisterRequest;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 前台用户认证控制器
 */
@Tag(name = "前台用户认证接口", description = "包括登录、注册等操作")
@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
public class AppAuthController {

    private final AppUserService appUserService;

    /**
     * 用户注册
     */
    @Operation(summary = "用户注册", description = "注册新用户")
    @PostMapping("/register")
    public Result<Long> register(@Valid @RequestBody RegisterRequest registerRequest) {
        Long userId = appUserService.register(registerRequest);
        return Result.success("注册成功", userId);
    }

    /**
     * 用户登录
     */
    @Operation(summary = "用户登录", description = "获取访问令牌")
    @PostMapping("/login")
    public Result<LoginResponse> login(@Valid @RequestBody LoginRequest loginRequest) {
        LoginResponse loginResponse = appUserService.login(loginRequest);
        return Result.success("登录成功", loginResponse);
    }

    /**
     * 用户登出
     */
    @Operation(summary = "用户登出", description = "退出登录")
    @PostMapping("/logout")
    public Result<String> logout() {
        StpUtil.logout();
        return Result.success("登录成功");
    }

    /**
     * 获取当前登录用户信息
     */
    @Operation(summary = "获取当前用户信息", description = "获取当前登录用户信息")
    @GetMapping("/user/info")
    public Result<LoginResponse> getCurrentUser() {
        LoginResponse userInfo = appUserService.getCurrentUser();
        return Result.success(userInfo);
    }
} 